home *** CD-ROM | disk | FTP | other *** search
- // -------------------------------------------------------------------------------------
- // user information functions
- // -------------------------------------------------------------------------------------
- // Permission is granted to freely redistribute this source code, and to use fragments
- // of this code in your own applications if you find them to be useful. This class/module,
- // along with the source code, come with no warranty of any kind, and the user assumes
- // all responsibility for its use.
- // -------------------------------------------------------------------------------------
-
- #import <stdio.h>
- #import <string.h>
- #import <ctype.h>
- #import <libc.h>
- #import <pwd.h>
- #import <sys/types.h>
- #import <sys/stat.h>
- #import <sys/param.h>
- #import <sys/dir.h>
- #import <mach/cthreads.h>
- #import <mach/mach_traps.h>
-
- // -----------------------------------------------------------------------------------------
- // future use
- #define userLOCK /* no-op */
- #define userUNLOCK /* no-op */
-
- // -----------------------------------------------------------------------------------------
- // XUserGetpwnam() and XUserGetpwuid() are NOT thread safe!
-
- /* wrapper for getpwnam */
- struct passwd *XUserGetpwnam(const char *name)
- {
- struct passwd *pw;
- userLOCK;
- pw = name? getpwnam(name) : getpwuid(getuid());
- userUNLOCK;
- return pw;
- }
-
- /* wrapper for getpwuid */
- struct passwd *XUserGetpwuid(uid_t uid)
- {
- struct passwd *pw;
- userLOCK;
- pw = getpwuid(uid);
- userUNLOCK;
- return pw;
- }
-
- // -----------------------------------------------------------------------------------------
-
- /* return true if euid == root */
- int XUserIsRootEuid()
- {
- int isRoot;
- userLOCK;
- isRoot = (getpwnam("root")->pw_uid == geteuid())? 1 : 0;
- userUNLOCK;
- return isRoot;
- }
-
- // -----------------------------------------------------------------------------------------
-
- /* return true if user is valid */
- int XUserIsValid(const char *user)
- {
- struct passwd *pw;
- userLOCK;
- pw = user? getpwnam((char*)user) : getpwuid(getuid());
- userUNLOCK;
- return pw? 1: 0;
- }
-
- /* return current user name */
- char *XUserRealName(const char *user, char *realName)
- {
- char *rtn = (char*)NULL;
- if (realName) {
- struct passwd *pw;
- userLOCK;
- if (!(pw = user? getpwnam((char*)user) : getpwuid(getuid()))) *realName = 0;
- else {
- char *bp = realName, *gp = pw->pw_gecos;
- while (*gp == '*') gp++;
- while (*gp && (*gp != ',')) *bp++ = *gp++;
- *bp = 0;
- rtn = realName;
- }
- userUNLOCK;
- }
- return rtn;
- }
-
- /* return user uid */
- uid_t XUserUid(const char *userName)
- {
- uid_t uid = -1;
- if (userName) {
- struct passwd *pw;
- userLOCK;
- if (pw = getpwnam(userName)) uid = pw->pw_uid;
- userUNLOCK;
- }
- return uid;
- }
-
- /* return user home directory */
- char *XUserHomeDirectory(const char *user, char *dir)
- {
- char *rtn = (char*)NULL;
- if (dir) {
- struct passwd *pw;
- userLOCK;
- if (!(pw = user? getpwnam((char*)user) : getpwuid(getuid()))) *dir = 0;
- else rtn = strcpy(dir, pw->pw_dir);
- userUNLOCK;
- }
- return rtn;
- }
-
- /* return current user name */
- char *XMyUserName(char *userName)
- {
- char *rtn = (char*)NULL;
- if (userName) {
- struct passwd *pw;
- userLOCK;
- if (!(pw = getpwuid(getuid()))) *userName = 0;
- else rtn = strcpy(userName, pw->pw_name);
- userUNLOCK;
- }
- return rtn;
- }
-
- /* return true if specified user is current user */
- int XIsCurrentUser(const char *userName)
- {
- int isCurrent = 0;
- if (userName) {
- struct passwd *pw;
- userLOCK;
- if ((pw = getpwnam(userName)) && (pw->pw_uid == getuid())) isCurrent = 1;
- userUNLOCK;
- }
- return isCurrent;
- }
-
- /* verify user password */
- int XUserVerifyPassword(const char *user, const char *password)
- {
- int ok = 0;
- struct passwd *pw;
- userLOCK;
- if (user && (pw = getpwnam((char*)user)) && pw->pw_passwd && *pw->pw_passwd &&
- !strcmp(crypt((char*)password, pw->pw_passwd), pw->pw_passwd)) ok = 1;
- userUNLOCK;
- return ok;
- }
-